﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity.Car_Rental;
using Entity;
using System.Timers;
namespace Data.Car_Rental
{
    public class ProductPropertyRepository : Car_RentalRepositoryBase, IProductPropertyRepository
    {

        #region IProductPropertyRepository 成员

        public IQueryable<ProductProperty> GetDetailModel()
        {
            var linq = from data in this.GetModel()
                       join data2 in new ProductPropertyTypeRepository().GetModel() on data.PropertyTypeID equals data2.ID
                       select new ProductProperty_Ext
                       {
                           Info = data.Info,
                           ProductPropertyType_Extend = data2,
                           PropertyID = data.PropertyID,
                           PropertyName = data.PropertyName,
                           PropertyTypeID = data.PropertyTypeID,
                       };
            return linq;
        }

        public Entity.PagedList<ProductProperty> GetProductProperty(VPredication vp, PagingParam pp)
        {
            var linq = this.GetDetailModel();
            return new PagedList<ProductProperty>(linq, pp.PageIndex, pp.PageSize);
        }
        #endregion

        #region IRepository<ProductProperty> 成员

        public void Update(ProductProperty entity)
        {
            base.UpdateEntity(entity);
        }

        public void Update(IList<ProductProperty> list)
        {
            base.UpdateEntity(list.Cast<EntityBase>().ToList());
        }

        public void Insert(ProductProperty entity)
        {
            base.InsertEntity(entity);
        }

        public void Insert(IList<ProductProperty> list)
        {
            base.InsertEntity(list.Cast<EntityBase>().ToList());
        }

        public ProductProperty InsertGetIDENTITY(ProductProperty entity)
        {
            base.InsertEntity(entity);
            return this.Find(entity.PrimaryKey);
        }

        public void Delete(ProductProperty entity)
        {
            base.DeleteEntity(entity);
        }

        public void Delete(IList<ProductProperty> list)
        {
            base.DeleteEntity(list.Cast<EntityBase>().ToList());
        }

        public IQueryable<ProductProperty> GetModel()
        {
            return base.GetEntities<ProductProperty>();
        }

        public ProductProperty Find(params object[] keyValues)
        {
            return base.Find<ProductProperty>(keyValues);
        }

        #endregion

        #region 缓存
        static Timer sysTimer = new Timer(3600000);
        /// <summary>
        /// 数据实体
        /// </summary>
        public static volatile List<ProductProperty> Instance = null;
        // Lock对象，线程安全所用
        private static object syncRoot = new Object();
        /// <summary>
        /// 类构造方法
        /// </summary>
        static ProductPropertyRepository()
        {
            ReLoad();
            sysTimer.AutoReset = true;
            sysTimer.Enabled = true;
            sysTimer.Elapsed += new ElapsedEventHandler(sysTimer_Elapsed);
            sysTimer.Start();
        }
        static void sysTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            ReLoad();
        }
        /// <summary>
        /// 重新加载数据结果集
        /// </summary>
        /// <returns></returns>
        public static List<ProductProperty> ReLoad()
        {
            lock (syncRoot)
            {
                Instance = new ProductPropertyRepository().GetModel().ToList();
            }
            return Instance;
        }
        #endregion
    }
}
